//app.js
var storage = require("utils/storage.js");
var api = require("./api.js");
var infoUtils = require("utils/userInfo.js");
const request = require('./utils/request_util.js')
import getData from './utils/agriknow.js'

App({
  request: request,
  onLaunch: function () {
    //隐藏系统tabbar
    // wx.hideTabBar();
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var _this = this;
    // _this.getWxUserInfo();
  },
  getOpenId:function(){
    const openid = this.globalData.wxUserInfo.openId;
    return openid;
  },
  getData:new getData(),
  onShow: function () { },
  onReady:function(){ },

  // 查看是否授权
  is_empower: function () {
    var _this = this;
    /** 获取open_id */
    var openid = _this.globalData.wxUserInfo.openId;
    console.log('open_id = ' + openid)
    /** 如果open_id 为 undefined 则获取用户信息 */
    if (openid == undefined) {
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getWxUserInfo 获取头像昵称
            _this.getWxUserInfo();
          } else {
            wx.navigateTo({
              url: '/pages/welcome/welcome',
            })
          }
        }
      })
    }
  },

  getWxUserInfo: function (e) {
    console.log(e)
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.globalData.systeminfo = res;
      },
    })
    return new Promise(function (resolve, reject) {
      if (_this.globalData.userInfo == {} || JSON.stringify(_this.globalData.userInfo) == '{}') {
        //调用登录接口
        wx.login({
          success: function (redd) {
            if (redd.code) {
              wx.request({
                url: api.login.getOpenId + '/' + redd.code,
                success: function (back) {
                  console.log(back)
                  if (back.statusCode == 200 ) {
                    var openId = back.data.data;
                    wx.getUserInfo({
                      lang:'zh_CN',
                      success: function (result) {
                        // console.log(result)
                        let wxUserInfo = result.userInfo;
                        _this.globalData.wxUserInfo.openId = openId;
                        wx.request({
                          url: api.login.login,
                          data: {
                            openId: openId,
                            nickname: wxUserInfo.nickName,
                            avatarUrl: wxUserInfo.avatarUrl,
                            gender: wxUserInfo.gender,
                            province: wxUserInfo.province,
                            city: wxUserInfo.city,

                          },
                          header: {
                            'content-type': 'application/x-www-form-urlencoded' // 默认值
                          },
                          method: 'post',
                          success: function (backResult) {
                            console.log(backResult)
                            if (backResult.statusCode == 200 && backResult.data.code == 200) {
                              _this.globalData.userInfo = backResult.data.data;
                            }
                            resolve(200);
                          }
                        })
                      },
                      fail: function (res) {
                        // console.log(res);
                        console.log('获取用户信息失败')
                        resolve()
                        //获取用户信息失败后。请跳转授权页面
                        // reject(600);
                      }
                    })
                  } else {
                    console.log("getOpenIdERR:", back);
                  }
                }
              })
            } else {
              console.log('获取用户信息失败');
              // reject(600);
              //获取用户信息失败后。请跳转授权页面
              wx.showModal({
                title: '警告',
                content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
                success: function (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/welcome/welcome',
                    })
                  }
                }
              })
            }

          }
        })

      } else {
        resolve(200);
      }
    });

  },
  globalData: {
    defaultCity: null,
    defaultCounty: null,
    wxUserInfo: {},
    userInfo: {},
    page: null,
    time:null,
    type:'',
    userId:'',
    systeminfo: {},
  }
});